package cn.alm.allinoneboot.controller;

import cn.alm.allinoneboot.common.CommonResult;
import cn.alm.allinoneboot.dto.CommonDto;
import cn.alm.allinoneboot.entity.Brand;
import cn.alm.allinoneboot.entity.Product;
import cn.alm.allinoneboot.service.ApiService;
import cn.alm.allinoneboot.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("api/product")
@CrossOrigin(origins = "*", maxAge = 3600)
@Slf4j
public class ProductController {

    @Resource
    ProductService productService;

    @Resource
    ApiService apiService;

    // 查询产品列表分页
    @PostMapping("getProductListPage")
    public CommonResult getProductListPage(@RequestHeader(value = "authorization", required = false) String authorization, @RequestBody CommonDto dto) {
        if (!apiService.tokenValid(authorization)) {
            CommonResult cr = new CommonResult();
            cr.unAuth();
            return cr;
        }
        return productService.getProductListPage(dto.getName(), dto.getCurrent(), dto.getSize());
    }

    // 查询全部产品
    @PostMapping("getAllProductList")
    public CommonResult getAllProductList(@RequestHeader(value = "authorization", required = false) String authorization) {
        if (!apiService.tokenValid(authorization)) {
            CommonResult cr = new CommonResult();
            cr.unAuth();
            return cr;
        }
        List<Product> allProductList = productService.getAllProductList();
        CommonResult cr = new CommonResult();
        cr.ok();
        cr.setData(allProductList);
        return cr;
    }

    // 根据产品id获取产品
    @PostMapping("getProductById")
    public CommonResult getProductById(@RequestHeader(value = "authorization", required = false) String authorization, @RequestBody Product product) {
        if (!apiService.tokenValid(authorization)) {
            CommonResult cr = new CommonResult();
            cr.unAuth();
            return cr;
        }
        return productService.getProductById(product.getId());
    }

    // 新增产品
    @PostMapping("add")
    public CommonResult addProduct(@RequestHeader(value = "authorization", required = false) String authorization, @RequestBody Product product) {
        if (!apiService.tokenValid(authorization)) {
            CommonResult cr = new CommonResult();
            cr.unAuth();
            return cr;
        }
        return productService.addProduct(product);
    }


    // 编辑产品
    @PostMapping("update")
    public CommonResult updateProduct(@RequestHeader(value = "authorization", required = false) String authorization, @RequestBody Product product) {
        if (!apiService.tokenValid(authorization)) {
            CommonResult cr = new CommonResult();
            cr.unAuth();
            return cr;
        }
        return productService.updateProduct(product);
    }

    // 删除商品
    @PostMapping("deleteProductById")
    public CommonResult deleteProductById(@RequestHeader(value = "authorization", required = false) String authorization, @RequestBody Product product) {
        if (!apiService.tokenValid(authorization)) {
            CommonResult cr = new CommonResult();
            cr.unAuth();
            return cr;
        }
        return productService.deleteProductById(product);
    }
}
